package com.thinkingstar.iads.common.utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SwapUtil {
	
	//清空List<Map>集合中为null的value值
	public static void clearNull(List <Map> list){
		for(Map map : list){
			Set<Map.Entry<String, Object>> entrySet = map.entrySet();
			for(Map.Entry<String, Object> entry : entrySet){
				if(entry.getValue() == null){
					entry.setValue("");
				}
			}
		}
	}
	
	/**
	 * 
	 * @param list   任意list集合 
	 * @param types  排序的顺序数据
	 * @param field  字段名称
	 */
	public static void swap(List<?> list,String[] types,String field){
		for (int i = 0; i < types.length; i++) {//顺序规则
			for (Object obj : list) { //调换顺序
				String getStr = field.substring(0, 1).toUpperCase() + field.substring(1);
				Class<? extends Object> class1 = obj.getClass();
				try {
					Method md = class1.getMethod("get" + getStr);
					if(md.invoke(obj).toString().equals(types[i].trim())){
						Collections.swap((List<?>) list, list.indexOf(obj), i);
						break;
					}
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
		}
	}
	
	/**
	 * 
	 * @param list   任意list集合 
	 * @param types  排序的顺序数据
	 * @param field  字段名称
	 */
	public static void swaps(List<?> list,String[] types,String field){
		int s=0;
		for (int i = 0; i < types.length; i++) {//顺序规则
			for (Object obj : list) { //调换顺序
				String getStr = field.substring(0, 1).toUpperCase() + field.substring(1);
				Class<? extends Object> class1 = obj.getClass();
				try {
					Method md = class1.getMethod("get" + getStr);
					if(md.invoke(obj).toString().equals(types[i].trim())){
					//	newList.add(obj);
						Collections.swap((List<?>) list, list.indexOf(obj), s++);
					}
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
		}
	}
}
